package org.spiderflow.aria.util;

/**
 * aria 下载类
 */

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.*;

/**
 * aria2请求的Json对象
 *
 * @author bx002
 * @date 2020/11/27 17:20
 */
//lombok注解  提供所有getter setter方法
@Data
//开启setter方法的链式调用
@Accessors(chain = true)
//无参构造方法
@NoArgsConstructor
@Slf4j
public class AriaDownloader {


    /**
     * 方法名常量
     */
    public final static String METHOD_TELL_ACTIVE = "aria2.tellActive";
    public final static String METHOD_ADD_URI = "aria2.addUri";
    public final static String METHOD_GET_GLOBAL_STAT = "aria2.getGlobalStat";
    /**
     * id随机生成，也可以手动设置
     */
    private String id = UUID.randomUUID().toString();
    private String jsonrpc = "2.0";
    private String method = METHOD_TELL_ACTIVE;
    private String url;
    private String token;
    //暂存下载参数
    private Map<String, Object> paramsMap = new HashMap<>();

    /**
     * 添加下载参数
     *
     * @param key
     * @param value
     * @return
     */
    public AriaDownloader addParam(String key, String value) {
        paramsMap.put(key, value);
        return this;
    }

    /**
     * 设置referer
     *
     * @param referer
     * @return
     */
    public AriaDownloader setReferer(String referer) {
        return addParam("referer", referer);
    }

    /**
     * 设置origin
     *
     * @param origin
     * @return
     */
    public AriaDownloader setOrigin(String origin) {
        return addHeader("Origin", origin);
    }

    /**
     * 设置下载根目录
     *
     * @param dir
     * @return
     */
    public AriaDownloader setDir(String dir) {
        if (StringUtils.isBlank(dir)) {
            dir = "/downloads";
        } else {
            dir = "/downloads/" + dir;
        }
        return addParam("dir", dir);
    }

    /**
     * 设置根目录下的子目录名称
     *
     * @param fileName
     * @return
     */
    public AriaDownloader setOutName(String fileName) {
        return addParam("out", fileName);
    }

    /**
     * 获取header列表
     *
     * @return
     */
    public List<String> findHeaders() {
        List<String> header = (List<String>) paramsMap.get("header");
        if (header == null) {
            header = new ArrayList<>();
            paramsMap.put("header", header);
        }
        return header;
    }

    /**
     * 添加header
     *
     * @param key
     * @param value
     */
    public AriaDownloader addHeader(String key, String value) {
        findHeaders().add(key + ": " + value);
        return this;
    }

    /**
     * 生成真正的params字段
     *
     * @return
     */
    public List<Object> getParams() {
        ArrayList<Object> list = new ArrayList<>();
        if (StringUtils.isNotBlank(this.token)) {
            //第一个为token
            list.add("token:" + token);
        } else {
            list.add("token:kakasi110");
        }
        //第二个成员为 url
        list.add(new ArrayList<>(Collections.singletonList(url)));
        //第二个成员为下载参数
        list.add(paramsMap);

        return list;
    }


    public AriaDownloader(String id) {
        this.id = id;
    }

    public String send(String jsonRpcUrl) throws IOException {
        //rpcurl 默认为本地默认地址
        jsonRpcUrl = StringUtils.isEmpty(jsonRpcUrl) ? "http://arirpc.acgfun.site/jsonrpc" : jsonRpcUrl;
        String jsonStr = JSONObject.toJSONString(this);
        //创建post请求对象
        String result = HttpUtil.createPost(jsonRpcUrl).body(jsonStr).execute().body();
        return result;
    }

    public static void main(String[] args) throws IOException {
        String url = "https://50069.njc.svp.tencent-cloud.com/gzc_1000035_0b53ueaocaaajuaktbje5brjliod4gqqbyka.f0.mp4?dis_k=192538f3150746bc403b406d916297ba&dis_t=1646546288";
        AriaDownloader aria2Json = new AriaDownloader();
        String result = aria2Json
                .setUrl(url)
                .setToken("kakasi110")
                .setMethod(aria2Json.METHOD_ADD_URI)
                .setDir("/downloads")
                .addParam("referer", "*")
                .send("http://arirpc.acgfun.site/jsonrpc");
        System.out.println("result = " + result);
    }


}
